package components.custom
{
	import flash.events.FocusEvent;
	import flash.ui.Keyboard;
	
	import mx.containers.FormItem;
	import mx.managers.IFocusManagerComponent;
	
	import spark.components.Group;
	import spark.layouts.HorizontalLayout;
	
	import interfaces.IDispose;
	
	import utils.ContainerUtil;
	
	public class ComplexFormContainer extends Group implements IDispose
	{
		/**储存表单的数组**/
		public var forms:Array = [];
		
		/**将要获取焦点的表单索引（forms的索引）**/
		private var _formIndex:int;
		
		/**将要获取焦点的控件索引**/
		private var _tabIndex:int;
		
		public function ComplexFormContainer()
		{
			super();
			layout = new HorizontalLayout();
			addEventListener(FocusEvent.KEY_FOCUS_CHANGE, focusChangeHandler);
		}
		
		public function dispose():void
		{
			removeEventListener(FocusEvent.KEY_FOCUS_CHANGE, focusChangeHandler);
		}
		
		/**
		 * 当按下tab键时获取当前内部焦点控件，如果控件存在（即焦点在本容器的内部）则查找下一个可以获取焦点的控件，并设置其成为新的焦点控件
		 * @param event
		 * 
		 */		
		private function focusChangeHandler(event:FocusEvent):void
		{
			if(event.keyCode == Keyboard.TAB){
				event.preventDefault();
				var iFocus:IFocusManagerComponent = focusManager.getFocus();
				if(iFocus){
					_formIndex = -1;
					_tabIndex = -1;
					searchCurrentIndex(iFocus);
					if(_formIndex >= 0 && _tabIndex >= 0){
						calculateNextIndex();
						//当新的焦点控件索引已超出范围时，将焦点设置成第一个表单的第一个控件
						if(!setCurrentIndex(_formIndex, _tabIndex))
							setCurrentIndex(0, 0);
					}
				}
			}
		}
		
		/**
		 * 搜索当前焦点的索引
		 * @param p_cmpt
		 * 
		 */		
		private function searchCurrentIndex(p_cmpt:IFocusManagerComponent):void
		{
			for(var i:int = 0;i < forms.length;i++){
				var _children:Array = ContainerUtil.getChildren(forms[i]);
				var index:int = -1;
				for(var j:int = 0;j < _children.length;j++){
					if(_children[j] is mx.containers.FormItem)
						index++;
					var iFocus:IFocusManagerComponent = _children[j] as IFocusManagerComponent;
					if(!iFocus)continue;
					if(iFocus == p_cmpt){
						_formIndex = i;
						_tabIndex = index;
						return;
					}
				}
			}
		}
		
		/**
		 * 计算新的焦点控件的索引
		 * 
		 */		
		private function calculateNextIndex():void
		{
			_formIndex++;
			if(_formIndex >= forms.length){
				_formIndex = 0;
				_tabIndex++;
			}
		}
		
		/**
		 * 设置新的焦点控件
		 * @param p_formIndex
		 * @param p_iFocusIndex
		 * @return 
		 * 
		 */		
		private function setCurrentIndex(p_formIndex:int, p_iFocusIndex:int):Boolean
		{
			var isSetFocus:Boolean = false;
			var _children:Array = ContainerUtil.getChildren(forms[p_formIndex]);
			var index:int = -1;
			for(var i:int = 0;i < _children.length;i++){
				if(_children[i] is mx.containers.FormItem)
					index++;
				var iFocus:IFocusManagerComponent = _children[i] as IFocusManagerComponent;
				if(!iFocus)continue;				
				if(index == p_iFocusIndex){
					iFocus.setFocus();
					isSetFocus = true;
					break;
				}
			}
			//trace(p_formIndex, p_iFocusIndex, "|", currentFormIndex, currentTabIndex);
			return isSetFocus;
		}
	}
}